package com.ruoyi.mng.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mng.domain.HzMerMerchant;
import com.ruoyi.mng.service.IHzMerMerchantService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商户信息Controller
 * 
 * @author chenqiang
 * @date 2023-04-26
 */
@Controller
@RequestMapping("/mng/merchant")
public class HzMerMerchantController extends BaseController
{
    private String prefix = "mng/merchant";

    @Autowired
    private IHzMerMerchantService hzMerMerchantService;

    @RequiresPermissions("mng:merchant:view")
    @GetMapping()
    public String merchant()
    {
        return prefix + "/merchant";
    }

    /**
     * 查询商户信息列表
     */
    @RequiresPermissions("mng:merchant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HzMerMerchant hzMerMerchant)
    {
        startPage();
        List<HzMerMerchant> list = hzMerMerchantService.selectHzMerMerchantList(hzMerMerchant);
        return getDataTable(list);
    }

    /**
     * 导出商户信息列表
     */
    @RequiresPermissions("mng:merchant:export")
    @Log(title = "商户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HzMerMerchant hzMerMerchant)
    {
        List<HzMerMerchant> list = hzMerMerchantService.selectHzMerMerchantList(hzMerMerchant);
        ExcelUtil<HzMerMerchant> util = new ExcelUtil<HzMerMerchant>(HzMerMerchant.class);
        return util.exportExcel(list, "商户信息数据");
    }

    /**
     * 新增商户信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商户信息
     */
    @RequiresPermissions("mng:merchant:add")
    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HzMerMerchant hzMerMerchant)
    {
        if(hzMerMerchant.getMerchantSignType().equals(2) && StringUtils.isEmpty(hzMerMerchant.getMerchantRsaPublickKey())){
            return AjaxResult.error("签名类型为RSA时 :: [商户RSA交换公钥] :: 必须填写");
        }
        return toAjax(hzMerMerchantService.insertHzMerMerchant(hzMerMerchant));
    }

    /**
     * 修改商户信息
     */
    @RequiresPermissions("mng:merchant:edit")
    @GetMapping("/edit/{merchantId}")
    public String edit(@PathVariable("merchantId") String merchantId, ModelMap mmap)
    {
        HzMerMerchant hzMerMerchant = hzMerMerchantService.selectHzMerMerchantByMerchantId(merchantId);
        mmap.put("hzMerMerchant", hzMerMerchant);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户信息
     */
    @RequiresPermissions("mng:merchant:edit")
    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HzMerMerchant hzMerMerchant)
    {
        return toAjax(hzMerMerchantService.updateHzMerMerchant(hzMerMerchant));
    }

    /**
     * 删除商户信息
     */
    @RequiresPermissions("mng:merchant:remove")
    @Log(title = "商户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(hzMerMerchantService.deleteHzMerMerchantByMerchantIds(ids));
    }
}
